package com.mashibing.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.mashibing.constant.IdentityConstant;
import com.mashibing.constant.TokenConstantsConfiguration;
import com.mashibing.dto.ResponseResult;
import com.mashibing.dto.TokenResult;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;


public class JwtUtils {

    private static  final String SIGN = "DSFP&@@";
    private static final String JWT_KEY_Phone = "phone";
//    身份 1、乘客  2、司机
    private static final String IDENTITY = "identity";

    private static final String TokenType = "tokenType";

    private static final String JWT_TOKEN_TIME = "jwtTokenTime";

    /**
     * 根据手机号生成token
     *
     * @param
     * @return
     */
    public static String generatorToken (String Phone,String identity,String tokenType){
        Map<String,String> map = new HashMap<>();
        map.put(JWT_KEY_Phone,Phone);
        map.put(IDENTITY,identity);
        map.put(TokenType,tokenType);
        // 防止每次生成的token都一样
        map.put(JWT_TOKEN_TIME, Calendar.getInstance().getTime().toString());

        // 创建过期时间   如果使用redis在服务端存储token 这里就不需要过期时间
//        Calendar calendar = Calendar.getInstance();
        // 设置过期时间一天
//        calendar.add(Calendar.DATE,1);
//        Date time = calendar.getTime();

        // 创建jwt

        JWTCreator.Builder builder = JWT.create();

        //整合Map
        map.forEach((k,v) -> {
            builder.withClaim(k,v);
        });

        //整合过期时间
//        builder.withExpiresAt(time);

        // 生成token
        return builder.sign(Algorithm.HMAC256(SIGN));


    }


    /**
     * 检查token是否有效
     * @param token
     * @return
     */
    public static TokenResult checkToken (String token){
        TokenResult tokenResult = null;
        try {
            tokenResult = JwtUtils.parseToken(token);
        }
        catch (Exception e) {
          return null;
        }
        return tokenResult;
    }

    /**
     * 解析token 并返回手机号
     * @param
     */

    public static TokenResult parseToken (String token){
        // 解析token
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
        // 获取中间部分
        String phone = verify.getClaim(JWT_KEY_Phone).asString();
        String identity = verify.getClaim(IDENTITY).asString();
        String tokenType = verify.getClaim(TokenType).asString();
        // 创建中间map输出
        TokenResult tokenResult = new TokenResult();
        tokenResult.setIdentity(identity);
        tokenResult.setPhone(phone);
        tokenResult.setTokenType(tokenType);
        return tokenResult;
    }

    public static void main(String[] args) {
        String token = generatorToken("18025306666",IdentityConstant.PASSENGER_IDENTITY, TokenConstantsConfiguration.ACCESS_TOKEN_TYPE);
        System.out.println(token);

        String parseToken = parseToken(token).toString();
        System.out.println(parseToken);
    }
}
